
#include "../lv_examples.h"
#if LV_BUILD_EXAMPLES && LV_USE_BTN

static lv_obj_t *btn_left;
static lv_obj_t *btn_right;
static lv_obj_t *btn_enter;

static void btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);
	lv_obj_t * label = lv_obj_get_child(btn, 0);

	static uint8_t cnt = 0;
    if(code == LV_EVENT_CLICKED) {
        cnt++;
    }

	if (btn == btn_left) {
		/*Get the first child of the button which is the label and change its text*/
        lv_label_set_text_fmt(label, "left: %d", cnt);
	} else if (btn == btn_right) {
		lv_label_set_text_fmt(label, "right: %d", cnt);
	} else if (btn == btn_enter) {
		lv_label_set_text_fmt(label, "enter: %d", cnt);
	}

	// LV_EVENT_KEY,                 /**< A key is sent to the object. Get the key with `lv_indev_get_key(lv_indev_get_act());`*/

	if (LV_EVENT_KEY == code) {
		uint32_t key = lv_indev_get_key(lv_indev_get_act());
		if (key == LV_KEY_LEFT) {
			LV_LOG_USER("---> LV_KEY_LEFT");
		} else if (key == LV_KEY_RIGHT) {
			LV_LOG_USER("---> LV_KEY_RIGHT");
		} else if (key == LV_KEY_ENTER) {
			LV_LOG_USER("---> LV_KEY_ENTER");
		} else {
			LV_LOG_USER("---> LV_KEY_XXX");
		}
	}
}

/**
 * Create a button with a label and react on click event.
 */
void lv_example_get_started_1(void)
{
    btn_left = lv_btn_create(lv_scr_act());     /*Add a button the current screen*/
    lv_obj_set_pos(btn_left, 30, 10);                            /*Set its position*/
    lv_obj_set_size(btn_left, 90, 30);                          /*Set its size*/
    lv_obj_add_event_cb(btn_left, btn_event_cb, LV_EVENT_ALL, NULL);           /*Assign a callback to the button*/

    lv_obj_t * label1 = lv_label_create(btn_left);          /*Add a label to the button*/
    lv_label_set_text(label1, "Left");                     /*Set the labels text*/
    lv_obj_center(label1);

	// 创建 btn_right
	btn_right = lv_btn_create(lv_scr_act());
	lv_obj_set_pos(btn_right, 130, 10);
	lv_obj_set_size(btn_right, 90, 30);
	lv_obj_add_event_cb(btn_right, btn_event_cb, LV_EVENT_ALL, NULL);

	lv_obj_t *label2 = lv_label_create(btn_right);
	lv_label_set_text(label2, "Right");
	lv_obj_center(label2);

	// 创建 btn_enter
	btn_enter = lv_btn_create(lv_scr_act());
	lv_obj_set_pos(btn_enter, 30, 70);
	lv_obj_set_size(btn_enter, 190, 30);
	lv_obj_add_event_cb(btn_enter, btn_event_cb, LV_EVENT_ALL, NULL);

	lv_obj_t *label3 = lv_label_create(btn_enter);
	lv_label_set_text(label3, "Enter");
	lv_obj_center(label3);

#if 1
	// 创建按键组
	extern lv_indev_t * indev_keypad;
	lv_group_t *g = lv_group_create();
	lv_indev_set_group(indev_keypad, g); // 将组绑定到输入设备
	lv_group_set_editing(g, false); // 非编辑模式，即导航模式
	lv_group_add_obj(g, btn_left);
	lv_group_add_obj(g, btn_right);
	lv_group_add_obj(g, btn_enter);
#endif
}

#endif
